package com.tvb.infoapp;

import java.io.File;
import java.util.List;

import com.tvb.infoapp.database.DatabaseHelper;
import com.tvb.infoapp.database.DatabaseHelperP;
import com.tvb.infoapp.object.CLSB_Field;
import com.tvb.infoapp.object.CLSB_Record;
import com.tvb.infoapp.object.CLSB_Structure;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {
    
   private DatabaseHelperP db;
   private ProgressDialog pDialog;
   private Button b_get_record, b_like_record, b_insert;
   private Long start_time, end_time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        if (new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/TVB/InfoApp/infoapp.sqlite").exists()) {
//            new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/TVB/InfoApp/infoapp.sqlite").delete();
//        }
        db = new DatabaseHelperP();
//        if (db.checkRecordDataBase()) {
//            Log.i("DATABASE EXIST", "true");
//        }else {
//            new AsyctaskInsertStructure().execute();
//        }
        
        initView();
    }
    
    private void initView() {
        // TODO Auto-generated method stub
        b_get_record = (Button) findViewById(R.id.b_get_record);
        b_get_record.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new AsyctaskSelectRecord().execute();
            }
        });
        b_like_record = (Button) findViewById(R.id.b_like_record);
        b_like_record.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new AsyctaskLikeRecord().execute();
            }
        });
        b_insert = (Button) findViewById(R.id.b_insert);
        b_insert.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new AsyctaskInsertStructure().execute();
            }
        });
    }
    
    public class AsyctaskSelectRecord extends AsyncTask<String, String, Boolean>{
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Selecting Record ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
            start_time = System.currentTimeMillis();
            db.selectRecord(1);
            return null;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            end_time = System.currentTimeMillis();
            pDialog.dismiss();
            Log.i("SELECT END", "Select Record success:" + (end_time - start_time));
            Toast.makeText(MainActivity.this, "Select Record success:" + (end_time - start_time), Toast.LENGTH_LONG).show();
        }
    }
    
    public class AsyctaskLikeRecord extends AsyncTask<String, String, Boolean>{
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Like Record ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
            start_time = System.currentTimeMillis();
            db.getListRecordByKeyword("Record 1");
            return null;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            end_time = System.currentTimeMillis();
            pDialog.dismiss();
            Log.i("SELECT END", "Select Record success:" + (end_time - start_time));
            Toast.makeText(MainActivity.this, "Select Record success:" + (end_time - start_time), Toast.LENGTH_LONG).show();
        }
    }

    public class AsyctaskInsertStructure extends AsyncTask<String, String, Boolean>{
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Inserting Structure table ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
            for (int i = 0; i < 100; i++) {
                db.insertStructure(new CLSB_Structure("Structure " + i, "Description " + i));
                Log.i("Insert Structure", "Structure " + i);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pDialog.dismiss();
            Toast.makeText(MainActivity.this, "Insert Structure success", Toast.LENGTH_SHORT).show();
            new AsyctaskInsertField().execute();
        }
        
        
    }
    
    public class AsyctaskInsertField extends AsyncTask<String, String, Boolean>{
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Inserting Field table ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
            List<CLSB_Structure> list_structure = db.getAllStructure();
            for (int i = 0; i < list_structure.size(); i ++) {
                CLSB_Structure structure = list_structure.get(i);
                for (int j = 0;  j < 5; j++) {
                    db.insertField(new CLSB_Field(structure.getId(),
                            "Field " + j + structure.getName(), "", 0, 0));
                    Log.i("Insert Field", "Field " + j + structure.getName());
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pDialog.dismiss();
            Toast.makeText(MainActivity.this, "Insert Field success", Toast.LENGTH_SHORT).show();
            new AsyctaskInsertRecord().execute();
        }
        
        
    }
    
    public class AsyctaskInsertRecord extends AsyncTask<String, String, Boolean>{
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Inserting Record table ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
            List<CLSB_Structure> list_structure = db.getAllStructure();
            for (int i = 0; i < list_structure.size(); i ++) {
                CLSB_Structure structure = list_structure.get(i);
                for (int j = 0;  j < 100; j++) {
                    db.insertRecord(new CLSB_Record(structure.getId(),
                            "Record " + j + "/ Structure" + i, "", "", "", "", ""));
                    Log.i("Insert Record", "Record " + j + "/ Structure" + i);
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pDialog.dismiss();
            Toast.makeText(MainActivity.this, "Insert Record success", Toast.LENGTH_SHORT).show();
            new AsyctaskInsertRecordField().execute();
        }
        
        
    }
    
    public class AsyctaskInsertRecordField extends AsyncTask<String, String, Boolean>{
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Inserting Record Field table ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
            List<CLSB_Record> list_record = db.getAllRecord();
            for (int i = 0; i < list_record.size(); i ++) {
                CLSB_Record record = list_record.get(i);
                List<CLSB_Field> list_field = db.getListFieldByStructure(record.getStructure_id());
                for (int j = 0; j < list_field.size(); j++) {
                    CLSB_Field field = list_field.get(j);
                    db.insertRecordField(record.getId(), field.getId(), "value");
                    Log.i("Insert Record Field", "Record " + i + "/ Field" + j);
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pDialog.dismiss();
            Toast.makeText(MainActivity.this, "Insert Record Field success", Toast.LENGTH_SHORT).show();
        }
        
        
    }

}
